import Toast from '@vant/weapp/toast/toast';
let ajaxTimes = 0 // 解决多个接口同时请求的问题

const request = params => {
  ajaxTimes++
  console.log('ajaxTimes===', ajaxTimes)
  if (ajaxTimes === 1) {
    wx.showLoading({
      title: '',
      mask: true,
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
    });
  }

  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header: {
        'Content-type': 'application/json',
        // Authorization: `bearer ${wx.getStorageSync(common.CC_TOKEN)}`
      },
      timeout: 30000,
      success: res => {
        // console.log('res===', res)
        if (res.data.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
          console.log(res)
        }
      },
      fail: err => {
        console.log('err', err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  }).catch(err => {
    console.log('errRequest===', err)
    Toast(err.data.msg)
  })
}

export default request